//
//  BaseNavigationView.m
//  CapriceMusic
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 MYX. All rights reserved.
//

#import "BaseNavigationView.h"
#import "BaseNavigationViewCell.h"
@implementation BaseNavigationView

- (instancetype)initWithFrame:(CGRect)frame withArray:(NSArray *)titleArray withJumpArray:(NSArray *)jumpArray{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
        self.array = jumpArray;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(self.frame.size.width / 5, 30);
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.minimumInteritemSpacing = CGFLOAT_MIN;
        flowLayout.minimumLineSpacing = CGFLOAT_MIN;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 30) collectionViewLayout:flowLayout];
        self.collectionView.bounces = NO;
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.contentOffset = CGPointMake(self.frame.size.width / 5 * 6, 0);
        [self.collectionView registerClass:[BaseNavigationViewCell class] forCellWithReuseIdentifier:@"BaseNavigationViewCellIdentifier"];
        [self addSubview:self.collectionView];
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseNavigationViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BaseNavigationViewCellIdentifier" forIndexPath:indexPath];
    [cell setTitle:self.titleArray[indexPath.item]];
    if (indexPath.item == 13) {
        if (self.one != 1) {
            NSIndexPath *index = [NSIndexPath indexPathForItem:12 inSection:0];
           BaseNavigationViewCell *cell = (BaseNavigationViewCell *)[self.collectionView cellForItemAtIndexPath:index];
            cell.titleLabel.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
            self.one = 1;
        }
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //    点击屏幕第一个
//    NSLog(@"%@",self.array[indexPath.row]);
    BaseNavigationViewCell *cell = (BaseNavigationViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    if (cell.frame.origin.x == collectionView.contentOffset.x) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x - (self.collectionView.bounds.size.width / 5 * 2), 0) animated:YES];
        //        点击屏幕最后一个
    } else if (cell.frame.origin.x - collectionView.contentOffset.x == self.collectionView.frame.size.width / 5 * 4) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x + (self.collectionView.bounds.size.width / 5 * 2), 0) animated:YES];
        //        点击屏幕第二个
    } else if (cell.frame.origin.x - collectionView.contentOffset.x == self.collectionView.frame.size.width / 5) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x - self.collectionView.bounds.size.width / 5, 0) animated:YES];
        //        点击屏幕倒数第二个
    } else if (cell.frame.origin.x - collectionView.contentOffset.x == self.collectionView.frame.size.width / 5 * 3) {
        [collectionView setContentOffset:CGPointMake(collectionView.contentOffset.x + self.collectionView.bounds.size.width / 5, 0) animated:YES];
    }
    [self.delegate sendRegion:self.array[indexPath.row]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x == self.collectionView.frame.size.width / 5 * 0) {
        [scrollView setContentOffset:CGPointMake(self.collectionView.frame.size.width / 5 * 6, 0)];
        //        向右滑
    } else if (scrollView.contentOffset.x == self.collectionView.frame.size.width / 5 * 13) {
        [scrollView setContentOffset:CGPointMake(self.collectionView.frame.size.width / 5 * 7, 0)];
    }
    NSLog(@"%f",self.collectionView.contentOffset.x);
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //    滑动结束时计算  当前偏移量／按钮长度，整除说明位置正好
    if ((NSInteger)scrollView.contentOffset.x % (NSInteger)(self.collectionView.frame.size.width / 5) == 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0)];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置大于item长度的一半，偏移量＋一个item的长度
    } else if(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) > 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) * (self.collectionView.frame.size.width / 5) + (self.collectionView.frame.size.width / 5), 0)  animated:YES];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置小于item长度的一半，偏移量修正
    } else if(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) < 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) * (self.collectionView.frame.size.width / 5), 0)  animated:YES];
    }
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    //    滑动结束时计算  当前偏移量／按钮长度，整除说明位置正好
    if ((NSInteger)scrollView.contentOffset.x % (NSInteger)(self.collectionView.frame.size.width / 5) == 0) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x, 0)];
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置大于item长度的一半，偏移量＋一个item的长度
    } else if(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) > 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) * (self.collectionView.frame.size.width / 5) + (self.collectionView.frame.size.width / 5), 0)  animated:YES];
        
        //    滑动结束时计算  当前偏移量／按钮长度，现在偏移量的位置小于item长度的一半，偏移量修正
    } else if(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5) - (NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) < 0.5) {
        [scrollView setContentOffset:CGPointMake((NSInteger)(scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5)) * (self.collectionView.frame.size.width / 5), 0)  animated:YES];
    }
}

//动画结束
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger num = scrollView.contentOffset.x / (self.collectionView.frame.size.width / 5) + 2;
    NSArray *array = [self.collectionView subviews];
    for (int i = 0; i < 6; i++) {
        BaseNavigationViewCell *cell = array[i];
        cell.titleLabel.textColor = [UIColor whiteColor];
    }
   
    NSIndexPath *index = [NSIndexPath indexPathForItem:num inSection:0];
    BaseNavigationViewCell *cell = (BaseNavigationViewCell *)[self.collectionView cellForItemAtIndexPath:index];
    cell.titleLabel.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    if (num == 9) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:16 inSection:0];
        BaseNavigationViewCell *cell = (BaseNavigationViewCell *)[self.collectionView cellForItemAtIndexPath:index];
        cell.titleLabel.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    }
    if (num == 8) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:14 inSection:0];
        BaseNavigationViewCell *cell = (BaseNavigationViewCell *)[self.collectionView cellForItemAtIndexPath:index];
        cell.titleLabel.textColor = [UIColor colorWithRed:30 / 255.0 green:195 / 255.0 blue:153 / 255.0 alpha:1.0];
    }
}
@end
